package com.g2room.driving.grap.life;

import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;

import com.g2room.driving.grap.life.model.po.ComShopComment;
import com.g2room.driving.grap.life.model.po.LifeImgs;
import com.g2room.driving.util.ImageUtil;
import com.g2room.myuall.util.DbUtil;

public class DownloadCtripImages implements Runnable {
	private static String dir = "E:/userdata/imags/1nsh/ctrip/";
	private static List<String> imgCommentNames = new ArrayList<String>();
	private static List<String> imgSmallNames = new ArrayList<String>();
	private static List<String> imgBigNames = new ArrayList<String>();
	private static List<String> imgHeadNames = new ArrayList<String>();
	private Map<String, String> map = new HashMap<String, String>();
	@Override
	public void run() {
		for(String url : map.keySet()) {
			downloadImg(url, map.get(url));
		}
	}
	
	public static void main(String[] args) {
		File files = new File(dir + "comment");
		if(files.exists()) {
			for(File file : files.listFiles()) {
				imgCommentNames.add(file.getName());
			}
		}
		files = null;
		files = new File(dir + "head");
		if(files.exists()) {
			for(File file : files.listFiles()) {
				imgHeadNames.add(file.getName());
			}
		}
		files = null;
		files = new File(dir + "b");
		if(files.exists()) {
			for(File file : files.listFiles()) {
				imgBigNames.add(file.getName());
			}
		}
		files = new File(dir + "s");
		if(files.exists()) {
			for(File file : files.listFiles()) {
				imgSmallNames.add(file.getName());
			}
		}
		
		List<ComShopComment> cscs = getCommentImgData();
		Map<String, String> map = new HashMap<String, String>();
		for(ComShopComment csc : cscs) {
			if(!StringUtil.isBlank(csc.getHead()) && !imgHeadNames.contains(getFn(csc.getHead()))) {
				map.put(csc.getHead(), "head");
			}
			String images = csc.getImages();
			if(StringUtil.isBlank(images)) continue;
			String[] imgs = images.split(";");
			for(String img : imgs) {
				if(!imgCommentNames.contains(getFn(img)))
				map.put(img, "comment");
			}
			if(map.size() >= 1000) {
				DownloadCtripImages dci = new DownloadCtripImages();
				dci.map.putAll(map);
				Thread t = new Thread(dci);
				t.start();
				map.clear();
			}
		}
		if(map.size() > 0) {
			DownloadCtripImages dci = new DownloadCtripImages();
			dci.map.putAll(map);
			Thread t = new Thread(dci);
			t.start();
			map.clear();
		}
		
		List<LifeImgs> lis = getImgData();
		for(LifeImgs li : lis) {
			if(!imgSmallNames.contains(getFn(li.getSmallImgUrl()))) map.put(li.getSmallImgUrl(), "s");
			if(!imgBigNames.contains(getFn(li.getBigImgUrl()))) map.put(li.getBigImgUrl(), "b");
			if(map.size() >= 1000) {
				DownloadCtripImages dci = new DownloadCtripImages();
				dci.map.putAll(map);
				Thread t = new Thread(dci);
				t.start();
				map.clear();
			}
		}
		if(map.size() > 0) {
			DownloadCtripImages dci = new DownloadCtripImages();
			dci.map.putAll(map);
			Thread t = new Thread(dci);
			t.start();
			map.clear();
		}
	}
	
	private static String getFn(String path) {
		return path.substring(path.lastIndexOf("/") + 1);
	}
	
	/**
	 * 获取已经抓取的了图片sql，从d_images
	 * @return
	 */
	public static List<ComShopComment> getCommentImgData() {
		String sql = "select id,head,images from life_comment order by id";
		List<ComShopComment> cscs = new ArrayList<ComShopComment>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				ComShopComment csc = new ComShopComment();
				csc.setHead(rs.getString("head"));
				csc.setImages(rs.getString("images"));
				csc.setId(rs.getInt("id"));
				cscs.add(csc);
			}
			rs.close();
			statement.close();
			DbUtil.closeConn(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cscs;
	}
	
	public static List<LifeImgs> getImgData() {
		String sql = "select id,small_img,big_img from life_imgs order by id";
		List<LifeImgs> cscs = new ArrayList<LifeImgs>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				LifeImgs csc = new LifeImgs();
				csc.setSmallImgUrl(rs.getString("small_img"));
				csc.setBigImgUrl(rs.getString("big_img"));
				csc.setId(rs.getInt("id"));
				cscs.add(csc);
			}
			rs.close();
			statement.close();
			DbUtil.closeConn(connection);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cscs;
	}
	
	public static void downloadImg(String imgUrl, String dir) {
		try {
			ImageUtil.downloadImageByType(imgUrl, dir);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
